// Copyright (c) 2021, yuxinyong and contributors
// For license information, please see license.txt

frappe.ui.form.on('Multi Level MRP', {
	onload: function(frm) {
		frm.get_field('items').grid.cannot_add_rows = true;
		frm.get_field('mrp_run_log').grid.cannot_add_rows = true;
		$('a.grid-upload.btn').hide();
		refresh_field('items');
		refresh_field('mrp_run_log');
	},
	setup: function (frm) {
		frm.set_query('sales_order', {
			'company': frm.doc.company,
			'docstatus': 1,
			'status': ['in',("To Deliver","To Deliver and Bill")]
		});
		frm.set_query('warehouse', {
			'disabled': 0,
			'mrp_exclude':0,
			'company': frm.doc.company,
			'is_group': 0
		});
		frm.set_query("mrp_bom_no", "sub_assembly_bom", function(doc, cdt, cdn) {
			const row = locals[cdt][cdn];
			return {
				"filters": {
					'company': frm.doc.company,
					'docstatus': 1,
					'is_active': 1,
					'name':['!=',row.bom_no],
					'item': row.item_code
				}
			};
		});
	},
	onload_post_render(frm){
		frm.get_field('get_plan_items') && frm.get_field('get_plan_items').$input && frm.get_field('get_plan_items').$input.addClass('btn-primary');		
	},
	refresh: function(frm) {
		$('a.grid-upload.btn').hide();
		if (frm.doc.items && !frm.is_new()) {
			frm.add_custom_button(__("Start Plan"), ()=> {
				frappe.call({
					method: "zelin_am.agile_manufacturing.doctype.multi_level_mrp.multi_level_mrp.run_mrp_multi_level_job",
					args:{
						mrp_doc_name: frm.doc.name,
						via_background_job: frm.doc.via_background_job
					},
					freeze: true,
					freeze_message: __("Plan running..."),
					callback: function(r) {
						frm.reload_doc();
					}
				});
			}).addClass('btn-primary');
		}
		if (frm.doc.items && frm.doc.mrp_run_log && frm.doc.mrp_run_log.length) {
			frm.add_custom_button(__("Work Order"), ()=> {
				Create_orders(frm, "Manufacture")
			}, __("Create"));
			frm.add_custom_button(__("Subcontract"), ()=> {
				Create_orders(frm, "Subcontract")
			}, __("Create"));
			frm.add_custom_button(__("Purchase Order"), ()=> {
				Create_orders(frm, "Purchase")
			}, __("Create"));
			frm.add_custom_button(__("Material Request"), ()=> {
				Create_orders(frm, "Customer Provided")
			}, __("Create"));
			frm.add_custom_button(__("All Orders"), ()=> {
				Create_orders(frm, "")
			}, __("Create"));
		}
		if (frm.doc.mrp_run_log && frm.doc.mrp_run_log.length) {
			frm.add_custom_button(__("Stock Requirements List"), ()=> {
				frappe.set_route('query-report', 'Stock Requirements List', {mrp: frm.doc.name});
			},__('View'));	
			frm.add_custom_button(__("Rescheduled Item"), ()=> {
				frappe.set_route('list', 'Rescheduled Item');
			},__('View'));	
		}	
	},
	get_plan_items: function(frm) {
		frappe.call({
			method: "get_plan_items",
			doc: frm.doc,
			freeze: true,
			freeze_message: __("running..."),
			callback: function(r) {
				refresh_field("items");
			}
		});
	},
	get_items_from: function(frm) {
		let show = frm.doc.get_items_from === "Material Request"? 0:1;
		frm.toggle_display("customer", show);
		frm.toggle_display("sales_order", show);
	}
});

var Create_orders = function(frm, supply_method){
	frappe.call({
		method: "zelin_am.agile_manufacturing.doctype.plan_order.plan_order.bulk_convert",
		args:{
			company: frm.doc.company,
			mrp: frm.doc.name,
			for_supply_method: supply_method
		},
		freeze: true,
		freeze_message: __("Converting to orders..."),
		callback: function(r) {
			if (r.message) frappe.msgprint(r.message);
		}
	});

}
